/*
 * Licensed Materials - Property of tenxcloud.com
 * (C) Copyright 2018 TenxCloud. All Rights Reserved.
 * 2018-06-13  @author lizhen
 */

package user

import "github.com/jinzhu/gorm"

type model struct {
	ID        int    `gorm:"column:user_id"`
	Name      string `gorm:"column:user_name"`
	Namespace string `gorm:"column:namespace"`
	Token     string `gorm:"column:api_token"`
	Role      int    `gorm:"column:role"`
}

func (model) TableName() string {
	return "tenx_users"
}

func (m model) GetUserID() int {
	return m.ID
}

func (m model) GetUserName() string {
	return m.Name
}

func (m model) GetUserToken() string {
	return m.Token
}

func (m model) GetRole() int {
	return m.Role
}

func (m model) GetNamespace() string {
	return m.Namespace
}

func FindOneByName(name string, db *gorm.DB) (user *model, err error) {
	var u model
	if err = db.Where("user_name = ?", name).First(&u).Error; err != nil {
		return
	}
	user = &u
	return
}
